package com.atguigu.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.order.bean.Order;
import com.atguigu.properties.OrderProperties;
import com.atguigu.service.OrderService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope // 激活nacos配置的自动刷新功能，配置中修改了，调用接口值也会变。不加注解不会变
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    OrderProperties orderProperties;



    @GetMapping("/config")
    public String config(){
        return "timeout:"+orderProperties.getTimeout()+
                ", autoConfirm:"+orderProperties.getAutoConfirm()+
                ", database:"+orderProperties.getDbUrl();
    }

    @GetMapping("/seckill")
    @SentinelResource(value = "seckill-order",fallback = "seckillFallback")
    public Order seckill(@RequestParam("userId") Long userId,
                          @RequestParam("productId") Long productId){
        Order order = orderService.getOrderById(userId,productId);
        return order;
    }

    public Order seckillFallback(Long userId, Long productId){
        Order order = new Order();
        order.setProductName("秒杀失败，走了fallback");
        order.setId(userId);
        order.setProductId(productId);
        return order;
    }


    @GetMapping("/order")
    public Order getOrder(@RequestParam("userId") Long userId,
                          @RequestParam("productId") Long productId){
        Order order = orderService.getOrderById(userId,productId);
        return order;
    }
}
